<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>库存信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_LocationId_FormItem" prop="Entity.LocationId">
								<template #label>
									<WtmLabelTooltip label="库位" tooltip="存储库位" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.LocationId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysWarehouseLocations" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MaterialId_FormItem" prop="Entity.MaterialId">
								<template #label>
									<WtmLabelTooltip label="物料" tooltip="库存物料" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.MaterialId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysMaterials" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_Quantity_FormItem"
								prop="Entity.Quantity"
								:rules="[{ required: true, message: '数量为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="数量" tooltip="库存数量" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.Quantity" disabled class="width100"></el-input-number>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SkuId_FormItem" prop="Entity.SkuId">
								<template #label>
									<WtmLabelTooltip label="物料SKU" tooltip="物料规格SKU" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.SkuId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysMaterialSkus" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_ErpId_FormItem" prop="Entity.ErpId">
								<template #label>
									<WtmLabelTooltip label="ERP编号" tooltip="ERP系统中的编号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.ErpId" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_BatchNumber_FormItem" prop="Entity.BatchNumber">
								<template #label>
									<WtmLabelTooltip label="批次号" tooltip="物料批次号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.BatchNumber" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PartnerId_FormItem" prop="Entity.PartnerId">
								<template #label>
									<WtmLabelTooltip label="供应商" tooltip="物料供应商" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.PartnerId" disabled class="width100">
									<el-option v-for="item in stateData.AllSysPartners" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_StoreType_FormItem"
								prop="Entity.StoreType"
								:rules="[{ required: true, message: '存储类型为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="存储类型" tooltip="存储类型" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.StoreType" disabled class="width100">
									<el-option key="受托代销" value="受托代销" label="受托代销"></el-option>
									<el-option key="客户来料" value="客户来料" label="客户来料"></el-option>
									<el-option key="客户寄存" value="客户寄存" label="客户寄存"></el-option>
									<el-option key="委托代销" value="委托代销" label="委托代销"></el-option>
									<el-option key="设备" value="设备" label="设备"></el-option>
									<el-option key="普通" value="普通" label="普通"></el-option>
									<el-option key="备品" value="备品" label="备品"></el-option>
									<el-option key="借出" value="借出" label="借出"></el-option>
									<el-option key="赠品" value="赠品" label="赠品"></el-option>
									<el-option key="其它" value="其它" label="其它"></el-option>
									<el-option key="借入" value="借入" label="借入"></el-option>
									<el-option key="供应商寄存" value="供应商寄存" label="供应商寄存"></el-option
								></el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysWarehouseStockApi as api } from '/@/views/supplychain/syswarehousestock/api';
import other from '/@/utils/other';
import { ISysWarehouseStockVm } from '/@/views/supplychain/syswarehousestock/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysWarehouseStockVm>>({
	vmModel: {
		Entity: {
			ID: null,
			LocationId: null,
			MaterialId: null,
			Quantity: null,
			SkuId: null,
			ErpId: null,
			BatchNumber: null,
			PartnerId: null,
			StoreType: '受托代销',
		},

		ActionName: null,
		IsMyApprove: null,
	},
	AllSysWarehouseLocations: [] as ISysSelectType[],
	AllSysMaterials: [] as ISysSelectType[],
	AllSysMaterialSkus: [] as ISysSelectType[],
	AllSysPartners: [] as ISysSelectType[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		other.getSelectList('/api/SysWarehouseStock/GetSysWarehouseLocations', [], false).then((x) => (stateData.AllSysWarehouseLocations = x)),
		other.getSelectList('/api/SysWarehouseStock/GetSysMaterials', [], false).then((x) => (stateData.AllSysMaterials = x)),
		other.getSelectList('/api/SysWarehouseStock/GetSysMaterialSkus', [], false).then((x) => (stateData.AllSysMaterialSkus = x)),
		other.getSelectList('/api/SysWarehouseStock/GetSysPartners', [], false).then((x) => (stateData.AllSysPartners = x)),
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
